package com.exoweb.common {
    import mx.core.Application;
    import mx.core.UIComponent;
    import mx.collections.ArrayCollection;
    import mx.managers.PopUpManager;

    /* Since this class is called on gameLoader, all sub-applications loaded
       by gameLoader will use this PopUpMessageManager
     */
    public class PopUpMessageManager {
        public static var windowList:ArrayCollection = new ArrayCollection();

        public static function popUpUnique(window:UIComponent,
                                        centerPopUp:Boolean=true,
                                        modal:Boolean=true):void {
            removeAll();
            addPopUp(window, centerPopUp, modal);
        }

        public static function addPopUp(window:UIComponent,
                                        centerPopUp:Boolean=true,
                                        modal:Boolean=true):void {
            var app:Application = Application(Application.application);
            windowList.addItem(window);
            PopUpManager.addPopUp(window, app, modal);
            if (centerPopUp) {
                PopUpManager.centerPopUp(window);
            }
        }

        public static function removePopUp(window:UIComponent):void {
            PopUpManager.removePopUp(window);
            var index:int = windowList.getItemIndex(window);
            if (index != -1) {
                windowList.removeItemAt(index);
            }
        }

        public static function removeAll():void {
            while (windowList.length > 0) {
                PopUpManager.removePopUp(windowList[0]);
                windowList.removeItemAt(0);
            }
        }
    }
}
